<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" pageEncoding="UTF-8" %>
<!DOCTYPE html >
<html>
<head>
    <base href="${base}/"/>
    <title>后台管理</title>
    <meta charset="utf-8"/>
    <link rel="stylesheet" href="css/admin.css"/>
    <style>
        /* 用户列表样式 */
        .search_form {
            margin-bottom: 20px;
            padding: 15px;
            background-color: #f9f9f9;
            border-radius: 5px;
        }

        .list_table {
            width: 100%;
            overflow-x: auto;
        }

        .list_table table {
            width: 100%;
            border-collapse: collapse;
        }

        .list_table th, .list_table td {
            padding: 10px;
            text-align: center;
            border: 1px solid #ddd;
        }

        .list_table th {
            background-color: #f2f2f2;
            font-weight: bold;
        }

        .list_table tr:nth-child(even) {
            background-color: #f9f9f9;
        }

        .list_table tr:hover {
            background-color: #f1f1f1;
        }

        /* 状态样式 */
        .status_normal {
            color: #28a745;
            font-weight: bold;
        }

        .status_frozen {
            color: #ffc107;
            font-weight: bold;
        }

        .status_blacklist {
            color: #dc3545;
            font-weight: bold;
        }

        /* 按钮样式 */
        .btn_freeze, .btn_unfreeze, .btn_blacklist, .btn_remove_blacklist, .btn_delete {
            display: inline-block;
            padding: 5px 10px;
            margin: 0 3px;
            border-radius: 3px;
            text-decoration: none;
            font-size: 12px;
        }

        .btn_freeze {
            background-color: #ffc107;
            color: #000;
        }

        .btn_unfreeze {
            background-color: #28a745;
            color: #fff;
        }

        .btn_blacklist {
            background-color: #dc3545;
            color: #fff;
        }

        .btn_remove_blacklist {
            background-color: #17a2b8;
            color: #fff;
        }

        .btn_delete {
            background-color: #6c757d;
            color: #fff;
        }

        /* 分页样式 */
        .pagination {
            margin-top: 20px;
            text-align: center;
        }

        .pagination a, .pagination span {
            display: inline-block;
            padding: 8px 12px;
            margin: 0 5px;
            border: 1px solid #ddd;
            border-radius: 3px;
            text-decoration: none;
            color: #333;
        }

        .pagination a:hover {
            background-color: #f1f1f1;
        }

        .pagination .page_current {
            background-color: #007bff;
            color: #fff;
            border-color: #007bff;
        }
    </style>
    <script type="text/javascript">
        // 下架商品
        function unfreezeUser(id) {
            if(confirm('确定要下架该商品吗？')) {
                window.location.href = 'admin/goods/unshelve?id=' + id;
            }
        }

        // 上架商品
        function freezeUser(id) {
            if(confirm('确定要上架该商品吗？')) {
                window.location.href = 'admin/goods/shelve?id=' + id;
            }
        }

        // 删除商品
        function deleteUser(id) {
            if(confirm('确定要删除该商品吗？此操作不可恢复！')) {
                window.location.href = 'admin/goods/delete?id=' + id;
            }
        }
    </script>
</head>
<body>
<div class="container">
    <div id="header">
        <div class="logo">
            <a href=""><img src="images/admin/logo.png" width="303" height="43" /></a>
        </div>
        <p>
            <a href="/user/logout">退出管理</a> <a href="/admin">后台首页</a> <a href="/" target='_blank'>商城首页</a>
            <span>您好 <label class='bold'>${sessionScope.manager.account}</label></span>
        </p>
    </div>

    <div id="admin_left">
        <ul class="submenu">
            <ul class="menu">
                <li><a href="admin/category/add">增加类别</a></li>
                <li><a href="admin/category/list">类别列表</a></li>
                <li><a href="admin/goods/add">增加商品</a></li>
                <li><a href="admin/goods/list">商品列表</a></li>
                <li><a href="admin/user/list">用户列表</a></li>

            </ul>
        </ul>
        <div id="copyright"></div>
    </div>

    <div id="admin_right">
        <div class="content_box">
            <!-- 商品列表 -->
            <div class="content">
                <div class="title">
                    <h2>商品列表</h2>
                </div>

                <!-- 搜索表单 -->
                <div class="search_form">
                    <form action="/admin/goods/list" method="get">
                        <table class="form_table" cellpadding="0" cellspacing="0">
                            <tr>
                                <th>商品名称：</th>
                                <td><input class="normal" name="name" type="text" value="${param.name}"></td>
                                <th>商品类别：</th>
                                <td>
                                    <select class="normal" name="categoryId">
                                        <option value="">全部</option>
                                        <c:forEach items="${categories}" var="category">
                                            <option value="${category.id}"
                                                ${param.categoryId == category.id ? 'selected' : ''}>
                                                    ${category.name}
                                            </option>
                                        </c:forEach>
                                    </select>
                                </td>
                                <th>价格范围：</th>
                                <td><input class="normal" name="maxPrice" type="number" placeholder="最高价" value="${param.maxPrice}"></td>
                                <td><input class="normal" name="minPrice" type="number" placeholder="最低价" value="${param.minPrice}"></td>
                                <td><button class="submit" type="submit">搜索</button></td>
                            </tr>
                        </table>
                    </form>
                </div>

                <!-- 商品列表表格 -->
                <div class="list_table">
                    <table cellpadding="0" cellspacing="0">
                        <thead>
                        <tr>
                            <th>图片</th>
                            <th>名称</th>
                            <th>类别</th>
                            <th>价格</th>
                            <th>库存</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <!-- 使用JSTL循环遍历商品列表 -->
                        <c:forEach items="${goodslist}" var="goods">
                            <tr>
                                <td><img src="${goods.image}" width="50" height="50"/></td>
                                <td>${goods.name}</td>
                                <td>${goods.categoryName}</td>
                                <td>${goods.salePrice}</td>
                                <td>${goods.stock}</td>
                                <td>
                                    <c:choose>
                                        <c:when test="${goods.status == '0'}">
                                            <span class="status_normal">在售</span>
                                        </c:when>
                                        <c:when test="${goods.status == '1'}">
                                            <span class="status_normal">下架</span>
                                        </c:when>
                                        <c:when test="${goods.status == '2'}">
                                            <span class="status_normal">已删除</span>
                                        </c:when>
                                    </c:choose>
                                </td>
                                <td>
                                    <c:choose>
                                        <c:when test="${goods.status == '0'}">
                                            <a href="javascript:void(0);" onclick="unfreezeUser('${goods.id}')" class="btn_unfreeze">下架</a>
                                        </c:when>
                                        <c:when test="${goods.status == '1' && goods.status != '2'}">
                                            <a href="javascript:void(0);" onclick="freezeUser('${goods.id}')" class="btn_freeze">上架</a>
                                        </c:when>
                                    </c:choose>
                                    <a href="javascript:void(0);" onclick="deleteUser('${goods.id}')" class="btn_delete">删除</a>
                                </td>
                            </tr>
                        </c:forEach>

                        </tbody>
                    </table>
                </div>

                <!-- 分页链接保留查询参数 -->
                <div class="pagination">
                    <c:if test="${currentPage > 1}">
                        <a href="/admin/goods/list?page=${currentPage - 1}&pageSize=${pageSize}
            &name=${param.name}&categoryId=${param.categoryId}
            &minPrice=${param.minPrice}&maxPrice=${param.maxPrice}"
                           class="page_prev">上一页</a>
                    </c:if>

                    <c:forEach begin="1" end="${totalPages}" var="i">
                        <a href="/admin/goods/list?page=${i}&pageSize=${pageSize}
            &name=${param.name}&categoryId=${param.categoryId}
            &minPrice=${param.minPrice}&maxPrice=${param.maxPrice}"
                           class="${currentPage == i ? 'page_current' : 'page_num'}">${i}</a>
                    </c:forEach>

                    <c:if test="${currentPage < totalPages}">
                        <a href="/admin/goods/list?page=${currentPage + 1}&pageSize=${pageSize}
            &name=${param.name}&categoryId=${param.categoryId}
            &minPrice=${param.minPrice}&maxPrice=${param.maxPrice}"
                           class="page_next">下一页</a>
                    </c:if>
                </div>
            </div>
        </div>
    </div>
    <div id="separator"></div>
</div>
</body>
</html>
